from basewindow import BaseWindow
from text_dialog import TextDialog
from defines import Defines

class ConfigWindow(BaseWindow):
    def __init__(self, program, widgets, config):
        BaseWindow.__init__(self, program, widgets, 'ConfigWindow', 'Config:Vbox', 'Config:Menubar')
        
        self.config = config
        
        # Fetch widgets.
        # Google config.
        self.google_username = widgets.get_widget('Config:GoogleUsername')
        self.google_password = widgets.get_widget('Config:GooglePassword')
        self.google_password2 = widgets.get_widget('Config:GooglePassword2')
        self.google_album = widgets.get_widget('Config:GoogleAlbum')
        # Mail config.
        self.mail_server = widgets.get_widget('Config:MailServer')
        self.mail_port = widgets.get_widget('Config:MailPort')
        self.mail_address = widgets.get_widget('Config:MailAddress')
        self.mail_auth = widgets.get_widget('Config:MailAuth')
        self.mail_username = widgets.get_widget('Config:MailUsername')
        self.mail_password = widgets.get_widget('Config:MailPassword')
        
        # Fetch buttons.
        save_button = widgets.get_widget('Config:SaveButton')
        cancel_button = widgets.get_widget('Config:CancelButton')
        
        # Connect buttons.
        save_button.connect('clicked', self.save_button_clicked)
        cancel_button.connect('clicked', self.cancel_button_clicked)
        self.mail_auth.connect('toggled', self.mail_auth_toggled)
    
        self.__set_windowed_mode()
        
    def save_button_clicked(self, widget):
        # Do sanity checks.
        # Check that the two passwords match in the Google tab.
        if self.google_password.get_text() != self.google_password2.get_text():
            TextDialog("Error", "Passwords for Google services does not match.").do_it()
            return
        
        # Store settings in the ConfigParser object.
        # Check whether Google settings are valid.
        if self.google_username.get_text() != '' and\
           self.google_password.get_text() != '' and\
           self.google_album.get_text() != '':
            if not self.config.has_section('google'):
                self.config.add_section('google')
            self.config.set('google', 'username', self.google_username.get_text())
            self.config.set('google', 'password', self.google_password.get_text())
            self.config.set('google', 'album', self.google_album.get_text())
        
        # Check and store the mail settings.
        if self.mail_server.get_text() != '' and\
           self.mail_port.get_text() != '' and\
           self.mail_address.get_text() != '':
            if not self.config.has_section('mail'):
                self.config.add_section('mail')
            self.config.set('mail', 'smtp_server', self.mail_server.get_text())
            self.config.set('mail', 'smtp_port', self.mail_port.get_text())
            self.config.set('mail', 'email_address', self.mail_address.get_text())
            if self.mail_auth.get_active():
                if self.mail_username.get_text() != '' and self.mail_password.get_text() != '':
                    self.config.set('mail', 'smtp_username', self.mail_username.get_text())
                    self.config.set('mail', 'smtp_password', self.mail_password.get_text())
            
        # Write the config to file.
        outfile = open(Defines.CONFIG_FILE, 'w')
        self.config.write(outfile)
        outfile.close()
        
        # Switch back to browser mode.
        self.program.set_mode('browse')
    
    def cancel_button_clicked(self, widget):
        self.program.set_mode('browse')
        
    def mail_auth_toggled(self, togglebutton):
        if togglebutton.get_active():
            self.mail_username.set_editable(True)
            self.mail_password.set_editable(True)
        else:
            self.mail_username.set_editable(False)
            self.mail_password.set_editable(False)
        
    def show(self):
        if self.fullscreen != self.program.fullscreen:
            self.toggle_fullscreen()
        self.window.show()
        
        # Read in settings from the config object.
        # Google settings.
        if self.config.has_section('google'):
            if self.config.has_option('google','username'):
                self.google_username.set_text(self.config.get('google','username'))
            if self.config.has_option('google','password'):
                self.google_password.set_text(self.config.get('google','password'))
                self.google_password2.set_text(self.config.get('google','password'))
            if self.config.has_option('google','album'):
                self.google_album.set_text(self.config.get('google','album'))
        # Mail settings.
        if self.config.has_section('mail'):
            if self.config.has_option('mail','smtp_server'):
                self.mail_server.set_text(self.config.get('mail','smtp_server'))
            if self.config.has_option('mail','smtp_port'):
                self.mail_port.set_text(self.config.get('mail','smtp_port'))
            if self.config.has_option('mail','email_address'):
                self.mail_address.set_text(self.config.get('mail','email_address'))
            if self.config.has_option('mail','smtp_username'):
                self.mail_username.set_text(self.config.get('mail','smtp_username'))
                self.mail_auth.set_active(True)
            if self.config.has_option('mail','smtp_password'):
                self.mail_password.set_text(self.config.get('mail','smtp_password'))
        
    def hide(self):
        # Store the fullscreen status in the program.
        self.program.fullscreen = self.fullscreen
        self.window.hide()

    def fullscreen_key_clicked(self):
        self.toggle_fullscreen()

    def __set_fullscreen_mode(self):
        self.fullscreen = True
        self.window.fullscreen()
        
    def __set_windowed_mode(self):
        self.fullscreen = False
        self.window.unfullscreen()

    def toggle_fullscreen(self):
        """
        Switches between fullscreen and windowed mode.
        """
        # Toggle between fullscreen and windowed mode.
        if self.fullscreen:
            self.__set_windowed_mode()
        else:
            self.__set_fullscreen_mode()
